package com.hotel.user.service;

import com.hotel.common.entity.Role;
import com.hotel.common.utils.Result;
import com.hotel.user.mapper.RoleMapper;
import com.hotel.user.mapper.RolePermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    public Result<Role> getRoleById(Long id) {
        Role role = roleMapper.findById(id);
        if (role == null) {
            return Result.error("角色不存在");
        }
        return Result.success(role);
    }

    public Result<Role> getRoleByCode(String roleCode) {
        Role role = roleMapper.findByRoleCode(roleCode);
        if (role == null) {
            return Result.error("角色不存在");
        }
        return Result.success(role);
    }

    public Result<List<Role>> getAllRoles() {
        List<Role> roles = roleMapper.findAll();
        return Result.success(roles);
    }

    public Result<Long> createRole(Role role) {
        // 检查角色编码是否已存在
        Role existingRole = roleMapper.findByRoleCode(role.getRoleCode());
        if (existingRole != null) {
            return Result.error("角色编码已存在");
        }
        
        Long roleId = roleMapper.insert(role);
        return Result.success(roleId);
    }

    public Result<Void> updateRole(Role role) {
        Role existingRole = roleMapper.findById(role.getId());
        if (existingRole == null) {
            return Result.error("角色不存在");
        }
        
        // 如果修改了角色编码，检查新编码是否已存在
        if (!existingRole.getRoleCode().equals(role.getRoleCode())) {
            Role roleWithSameCode = roleMapper.findByRoleCode(role.getRoleCode());
            if (roleWithSameCode != null) {
                return Result.error("角色编码已存在");
            }
        }
        
        roleMapper.update(role);
        return Result.success();
    }

    public Result<Void> deleteRole(Long id) {
        Role role = roleMapper.findById(id);
        if (role == null) {
            return Result.error("角色不存在");
        }
        
        // 删除角色的权限关联
        rolePermissionMapper.deleteByRoleId(id);
        // 删除角色
        roleMapper.deleteById(id);
        return Result.success();
    }

    public Result<Void> assignPermission(Long roleId, Long permissionId) {
        rolePermissionMapper.insert(roleId, permissionId);
        return Result.success();
    }

    public Result<Void> removePermission(Long roleId, Long permissionId) {
        rolePermissionMapper.deleteByRoleIdAndPermissionId(roleId, permissionId);
        return Result.success();
    }

    public Result<List<Role>> getRolesByUserId(Long userId) {
        List<Role> roles = roleMapper.findByUserId(userId);
        return Result.success(roles);
    }

    public Result<List<Role>> getRolesWithPermissions() {
        List<Role> roles = roleMapper.findAllWithPermissions();
        return Result.success(roles);
    }

    public Result<Void> assignPermissionsToRole(Long roleId, List<Long> permissionIds) {
        // 先删除角色的所有权限
        rolePermissionMapper.deleteByRoleId(roleId);
        
        // 批量添加新权限
        for (Long permissionId : permissionIds) {
            rolePermissionMapper.insert(roleId, permissionId);
        }
        
        return Result.success();
    }

    public Result<Void> removeAllPermissionsFromRole(Long roleId) {
        rolePermissionMapper.deleteByRoleId(roleId);
        return Result.success();
    }
}